<template>
    <el-dialog v-loading="loading" :visible="visible" :title="$t('inspectionManagement.selectTemplate')" width="600px" append-to-body top="14vh" class="knowledge-dialog" :close-on-click-modal="false" :before-close="close" @close="onClose" @open="onOpen">
        <div>
            <el-form :model="searchForm" size="small" label-position="left" label-suffix=":" ref="searchForm" label-width="80px">
                <el-row :gutter="24">
                    <el-col :span="12">
                        <el-form-item :label="$t('inspectionManagement.templateName')" prop="templateName">
                            <el-input v-model="queryParams.templateName" :placeholder="$t('public.inputPl')" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item style="text-align:right">
                            <el-button type="primary" icon="el-icon-search" @click="getList" class="search-btn" size="mini">{{ $t("public.search") }}</el-button>
                            <el-button icon="el-icon-refresh" @click="reset" size="mini">{{ $t("public.reset") }}</el-button>
                        </el-form-item>
                    </el-col>
                </el-row>
            </el-form>
            <div class="container-table">
                <el-table max-height="350" border stripe v-loading="loading" :data="templateList">
                    <el-table-column :label="$t('public.serialNum')" type="index" width="50" align="center">
                        <template slot-scope="scope">
                            <span>{{(queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1}}</span>
                        </template>
                    </el-table-column>
                    <el-table-column :label="$t('inspectionManagement.templateName')" align="left" prop="templateName" minWidth="160" />
                    <el-table-column :label="$t('public.opera')" align="center" width="100">
                        <template slot-scope="scope">
                            <a class="action-wrap" @click="choose(scope)" style="color:#5a9eaf">
                                {{$t('rule.select')}}
                            </a>
                        </template>
                    </el-table-column>
                </el-table>

                <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" layout="total, sizes, prev, pager, next, jumper" />
            </div>
        </div>
        <div slot="footer">
            <el-button size="mini" type="default" icon="el-icon-close" @click="handleConfirm">{{ $t("btn.close") }}</el-button>
        </div>
    </el-dialog>
</template>

<script>
import {
    listTemplate,
    getItemsByTemplateName,
} from '@/api/inspectionManagement/inspectionManagement'

export default {
    name: 'TemplateDialog',
    props: {
        visible: {
            type: Boolean,
            default: false,
        },
        params: {
            type: Object,
            default: null,
        },
    },
    data() {
        return {
            total: 1,
            templateList: [{ templateName: 'xx模板' }],
            searchForm: {
                templateName: null,
            },
            queryParams: {
                pageNum: 1,
                pageSize: 10,
                templateName: null,
            },
            loading: false,
            data: null,
            title: '',
            nodeType: this.$store.state.fmea.nodeType,
        }
    },
    methods: {
        reset() {
            this.queryParams = {
                pageNum: 1,
                pageSize: 10,
                templateName: null,
                dataSource: '1'
            }
            this.getList()
        },
        getList() {
            this.loading = true
            listTemplate(this.queryParams).then((response) => {
                if (response.code == 200 && response.rows) {
                    this.templateList = response.rows
                    this.total = response.total
                    this.loading = false
                }
            })
        },
        // 窗口打开
        onOpen() {
            this.queryParams.dataSource = '1'
            this.getList()
        },
        // 窗口关闭
        onClose() {
            this.data = null
        },
        handleConfirm() {
            this.$emit('update:visible', false)
        },
        choose(scope) {
            this.loading = true
            console.log(scope.row.templateName)
            getItemsByTemplateName({
                templateName: scope.row.templateName,
            }).then((response) => {
                console.log(response)
                this.$emit('callback', response.data)
                this.$emit('update:visible', false)
            }).finally(()=>{
                this.loading = false
            })
        },
        // 窗口关闭前执行
        close() {
            this.$emit('update:visible', false)
        },
    },
}
</script>

<style scoped>
::v-deep .el-dialog__footer {
    text-align: center;
}
.search-btn {
    background: #20b09f;
    border-color: #20b09f;
}
</style>
